<template>
  <!-- 恢复出厂设置弹框 -->
    <el-dialog v-model="dialogShow0" width="30%">
      <div class="titleShow">
        <div class="titles">{{ $t('licence.sys_restoreFactory') }}</div>
      </div>
      <el-form :inline="true" :model="formInline" :rules="rules" ref="formInlines" class="demo-form-inline">
        <el-form-item :label="$t('licence.sys_guojia')" prop="num_0">
          <el-input style="width: 300px" v-model="formInline.num_0"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogShow0 = false">{{ $t('licence.cancel') }}</el-button>
          <el-button type="primary" @click="instruction">{{ $t('licence.determine') }}</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { commandControl,getPsListByIds } from "@/api/inveter/inveter";
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
const dialogShow0 = ref(false) //恢复出厂设置
const formInlines = ref<ElFormInstance>();
const formInline = ref({
  num_0: '' as any,
})
const rules = reactive({
  num_0: [
    {
      required: true,
      message: t("licence.sys_shurushuzhi"),
      trigger: "blur",
    },
    {
      pattern: /^(?:0|[1-9][0-9]?|200)$/,
      message: "最大数值0-200之间",
      trigger: "blur",
    },
  ],
});
const env = import.meta.env.VITE_APP_ENV
const props = defineProps(['ids','lang','powerStation']);
console.log('props:', props)
const emits = defineEmits('compareResult');
const instruction = async ()=>{
  await formInlines.value?.validate();
    ElMessageBox.confirm(t("licence.recovery"), t("licence.Tips"), {
      confirmButtonText: t("licence.determine"),
      cancelButtonText: t("licence.cancel"),
      type: "warning",
    })
      .then(async () => {
        let data = {
          commandId: 0,
          ids: props.ids.join(","),
          setVal: formInline.value.num_0,
          lang: props.lang,
        };
        await commandControl(data);
        // 车间特有
        if(env=='staging'){
            if(formInline.value.num_0 > 20) {
                ElMessage({
                  type: "success",
                  message: t("licence.success"),
                });
            } else {
              ElMessage({
                type: "success",
                duration: 40000,
                message: t("licence.sys_restoreFactory_compare"),
              });
              setTimeout(async () => {
                let data = {
                  stationId: props.powerStation.id,
                  commandId: 12,
                  ids: -1,
                  lang: props.lang,
                };
                commandControl(data);
              }, 30000);
              setTimeout(async () => {
                let res = await getPsListByIds(props.ids.join(","));
                emits('compareResult',res.data,formInline.value.num_0);
              }, 40000);
            }
        }
        else ElMessage({
          type: "success",
          message: t("licence.success"),
        });
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: t("licence.cancels"),
        });
      });
    dialogShow0.value = false;
}
// 参数下发
// (参数弹框)
const openDialog = () => {
  dialogShow0.value = true;
  formInline.value.num_0 = "";
}
defineExpose({
  openDialog
})
</script>
